package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.ResultVO;
import com.ruoyi.system.utils.CachetokenUtil;
import com.ruoyi.system.utils.YsyToken;
import net.sf.ehcache.Cache;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysActivity;
import com.ruoyi.system.service.ISysActivityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author ruoyi
 * @date 2020-11-11
 */
@Controller
@RequestMapping("/system/activity")
public class SysActivityController extends BaseController
{
    private String prefix = "system/activity";

    @Autowired
    private ISysActivityService sysActivityService;


    @RequiresPermissions("system:activity:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/activity";
    }

    /**
     * 查询活动列表
     */
    @RequiresPermissions("system:activity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysActivity sysActivity)
    {
        startPage();
        List<SysActivity> list = sysActivityService.selectSysActivityList(sysActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @RequiresPermissions("system:activity:export")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysActivity sysActivity)
    {
        List<SysActivity> list = sysActivityService.selectSysActivityList(sysActivity);
        ExcelUtil<SysActivity> util = new ExcelUtil<SysActivity>(SysActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 新增活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @RequiresPermissions("system:activity:add")
    @Log(title = "新增活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysActivity sysActivity)
    {
        sysActivity.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysActivityService.insertSysActivity(sysActivity));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{activityId}")
    public String edit(@PathVariable("activityId") Integer activityId, ModelMap mmap)
    {
        SysActivity sysActivity = sysActivityService.selectSysActivityById(activityId);
        mmap.put("activity", sysActivity);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:activity:edit")
    @Log(title = "编辑活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysActivity sysActivity)
    {
        sysActivity.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysActivityService.updateSysActivity(sysActivity));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:activity:remove")
    @Log(title = "删除活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysActivityService.deleteSysActivityByIds(ids));
    }
}
